﻿<!DOCTYPE html>
<html>
<head>
  <title>ol-ext: control Legends</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="iconic font and scalable vector icons for OL3" />
  <meta name="keywords" content="ol3, style, vector, font, fontawesome, icon, maki" />

  <link rel="stylesheet" href="../style.css" />
  <link rel="stylesheet" href="../css/fontmaki.css" />
  <link rel="stylesheet" href="../css/fontmaki2.css" />
  <!-- FontAwesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      
  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>


  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <!-- ol-ext maki and fontawesome defintions used in fontsymbol -->
  <script type="text/javascript" src="../../dist/extra/FontAwesomeDef.js"></script>

  <style>
  .ol-legend > canvas {
    float: left;
  }
  .ol-legend > div.ol-legend {
    position: relative;
  }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <i clas="fa fa-car" style="position: absolute; top: -100px;"></i>

  <a href="../../index.html">
    <h1>ol-ext: control layer legends</h1>
  </a>
  <div class="info">
    <i>ol/legend/Legend</i> can be associated with a layer and displayed when the layer is visible only.
    <br/>
    This example displays 2 legends associated with layers.
  </div>

  <!-- DIV pour la carte -->
  <div id="map" style="width:600px; height:400px;"></div>

<script type="text/javascript">
  
  // Layers
  var layer = new ol.layer.Geoportail({ layer: 'GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2' });
  var geology = new ol.layer.Tile ({
    "title": "Cartes géologiques",
    "extent": [
      -20037508.342789244,
      -238107693.26496765,
      20037508.342789244,
      238107693.26496765
    ],
    "queryable": false,
    "abstract": "Cartes géologiques",
    "minResolution": 3.1750000000000003,
    "maxResolution": 3527.777777777778,
    "source": new ol.source.TileWMS({
      "url": "http://geoservices.brgm.fr/geologie?language=fre&",
      "projection": "EPSG:3857",
      "attributions": [
        "<a href='http://www.brgm.fr/'>&copy; Brgm</a>"
      ],
      "crossOrigin": "anonymous",
      "params": {
        "LAYERS": "GEOLOGIE",
        "FORMAT": "image/png",
        "VERSION": "1.3.0"
      }
    })
  })

  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View ({
      zoom: 7,
      center: [166326, 5992663]
    }),
    layers: [layer, geology]
  });

  map.addControl(new ol.control.LayerSwitcher({ collapsed: false }))

  // Define a new legend
  var legend = new ol.legend.Legend({ 
    title: 'Legend',
    margin: 5,
    maxWidth: 300
  });
  var legendCtrl = new ol.control.Legend({
    legend: legend,
    collapsed: false
  });
  map.addControl(legendCtrl);

  // New legend associated with a layer
  var vectorLegend = new ol.legend.Legend({ layer: layer, margin: 4 })
  vectorLegend.addItem({ title: 'Form' })
  var form = { Triangle:3, Square:4, Pentagon: 5, Hexagon: 6 };
  for (var i in form) {
    vectorLegend.addItem({ 
      title: i, 
      typeGeom: 'Point',
      style: new ol.style.Style({
        image: new ol.style.RegularShape({
          points: form[i],
          radius: 15,
          stroke: new ol.style.Stroke({ color: [255,128,0,1 ], width: 1.5 }),
          fill: new ol.style.Fill({ color: [255,255,0,.3 ]})
        })
      })
    });
  }

  // New legend associated with a layer
  var layerLegend = new ol.legend.Legend({ layer: geology })

  layerLegend.addItem(new ol.legend.Image({
    title: 'Geology',
    src: 'http://geoservices.brgm.fr/geologie?language=fre&version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=GEOSERVICES_GEOLOGIE&format=image/png&STYLE=default'
  }))

  legend.addItem(vectorLegend)
  legend.addItem(layerLegend)

</script>
  
</body>
</html>
